package com.yanqu.sync.demo.dispatcher;

import com.yanqu.sync.demo.entity.Request;
import com.yanqu.sync.demo.entity.Response;

import java.time.ZonedDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Service {

    private static final ExecutorService executorService = Executors.newFixedThreadPool(1);

    public static void clientSend(final Request request) {

        //这里模拟调用发送
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.printf("service ----req:%s----thread:%s----, time: %s\n", request.getSequenceId(), Thread.currentThread().getId(), ZonedDateTime.now());

                serviceResponse(request);
            }
        });
    }

    public static void serviceResponse(Request request) {
        Response response = new Response();
        response.setSequenceId(request.getSequenceId());
        response.setMessage("serviceResponse");
        PendingCenter.receive(response);
    }

    public static void  shutDown() {
        executorService.shutdown();
    }
}
